package com.kpmg.datalake.schedule.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-08-27 21:20:14
 */
public class ServerIpUtil {

  private ServerIpUtil() {
  }

  /**
   * 获取服务器IP列表
   *
   * @return ip 列表
   */
  public static List<String> getServerIp() {
    List<String> ipList = new ArrayList<>();
    try {
      Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
      while (netInterfaces.hasMoreElements()) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
          InetAddress inetAddress = inetAddresses.nextElement();
          ipList.add(inetAddress.getHostAddress());
        }
      }
    } catch (SocketException e) {
      e.printStackTrace();
    }
    return ipList;

  }

}
